/* 发送端代码 send.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>

#define GROUP_IP "224.0.0.1"    // 组播组IP地址
#define PORT 8765               // 端口号
#define MSG "Hello, Multicast!" // 发送的消息

int sockfd;
struct ip_mreq group;
struct sigaction act;

extern void handler_signal(int);
extern void signal_init();
extern int readInput_sendMulticast(int sockfd, struct sockaddr_in *group_addr);
extern int loop_sendMulticast(int sockfd, struct sockaddr_in *group_addr);

int main()
{
    int sockfd;
    struct sockaddr_in group_addr;
    int opt = 1;
    int loop = 1; // 开启组播回环
    signal_init();

    // 创建UDP套接字
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    // 设置套接字选项，允许发送组播数据包
    if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &opt, sizeof(opt)) < 0)
    {
        perror("setsockopt IP_MULTICAST_TTL failed");
        exit(EXIT_FAILURE);
    }

    // 设置组播回环选项
    if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
    {
        perror("setsockopt IP_MULTICAST_LOOP failed");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    // 设置组播地址和端口
    memset(&group_addr, 0, sizeof(group_addr));
    group_addr.sin_family = AF_INET;
    group_addr.sin_port = htons(PORT);
    // group_addr.sin_addr.s_addr = inet_addr(GROUP_IP);
    if (inet_pton(AF_INET, GROUP_IP, &group_addr.sin_addr) <= 0)
    {
        perror("invalid address/ address not supported");
        exit(EXIT_FAILURE);
    }

    // 发送组播消息
    loop_sendMulticast(sockfd, &group_addr);
    // read_input_to_multicast_group(sockfd, &group_addr);

    printf("Message sent successfully.\n");
    close(sockfd);
    return 0;
}


void handler_signal(int signal)
{
    printf("signal=%d \n", signal);
    // if (signal == SIGINT)
    {
        // 离开组播组
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &group, sizeof(group)) < 0)
        {
            perror("setsockopt failed");
            exit(EXIT_FAILURE);
        }
        close(sockfd);
    }

    exit(0);
}

void signal_init()
{
    act.sa_handler = handler_signal;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    sigaction(SIGINT, &act, 0);
}

int readInput_sendMulticast(int sockfd, struct sockaddr_in *group_addr)
{
    char input[64];
    while (1)
    {
        printf("Enter message to send (or 'exit' to quit): ");
        fgets(input, sizeof(input), stdin);
        input[strcspn(input, "\n")] = '\0'; // 去掉换行符

        if (strcmp(input, "exit") == 0)
        {
            break;
        }

        if (sendto(sockfd, input, strlen(input), 0,
                   (struct sockaddr *)group_addr, sizeof(*group_addr)) < 0)
        {
            perror("sendto failed");
            return -1;
        }
    }
    return 0;
}

int loop_sendMulticast(int sockfd, struct sockaddr_in *group_addr)
{
    char buffer[64] = {0};
    int counter = 0;
    // 循环发送消息
    while (1)
    {
        snprintf(buffer, sizeof(buffer)-1, "组播消息 #%d", counter++);
        if (sendto(sockfd, buffer, strlen(buffer), 0,
                   (struct sockaddr *)group_addr, sizeof(*group_addr)) < 0)
        {
            perror("sendto failed");
            return -1;
        }
        printf("发送: %s\n", buffer);
        sleep(1);
    }
}